package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.CartMapper;
import com.ktmall.ktmalluijava.pojo.Cart;
import com.ktmall.ktmalluijava.service.CarService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/10 上午8:33
 */
@Service
public class CartServiceImpl implements CarService {
    @Resource
    private CartMapper cartMapper;

    /**
     * 获取分页对象
     *
     * @param username
     * @param goodsName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Cart> getPage(String username, String goodsName, Integer pageNo, Integer pageSize) {
        Page<Cart> cartPage = new Page<>();
        cartPage.setPageSize(pageSize);
        cartPage.setTotalCount(cartMapper.count(username,goodsName));
        cartPage.setPageNo(pageNo);
        int offset = (cartPage.getPageNo()-1)*cartPage.getPageSize();
        cartPage.setData(cartMapper.select(username,goodsName,cartPage.getPageSize(),offset));
        return cartPage;
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer... id) {
        if(id.length==1){
            return cartMapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
            count += cartMapper.delete(integer);
        }
        return count;
    }
}
